﻿
@using Blazor.Diagrams.Components
@using AntDesign
@using AntDesign.ProLayout;
@using AntDesign.Core;
@using AntDesign.TableModels;
@using Blazor.Diagrams.Core.Models
@using ERP.Workflow.Designer.BlazorUI.Domain
@using  ERP.Workflow.Designer.BlazorUI.Models;
@inherits ComponentBase
 

<div Class="@Class" Style="@Style"   @onmousedown:stopPropagation="true"  >
    <DynamicComponent   @ref="dynProperty" Type="@_renderComponent" Parameters="@parameters"></DynamicComponent>
</div>
@code {

    [Parameter] public string Class { get; set; } = string.Empty;
    [Parameter] public string Style{ get; set; }= string.Empty;
    [CascadingParameter] public Diagram? Diagram{ get; set; }


    [Parameter] public EventCallback<PropertyChangeEventArg> OnChange{ get; set; }

    private Dictionary<Type, Type> keyValuePairs = new Dictionary<Type, Type>();
    private Dictionary<string, object> parameters = new Dictionary<string, object>()
    {
        {"Data",new FlowNode() }
    };
    private Type _renderComponent = typeof(NodePropertyBox);
    private DynamicComponent? dynProperty;

    protected override Task OnInitializedAsync()
    {
        Regist();
        return base.OnInitializedAsync();
    }
    protected virtual void Regist()
    {
        keyValuePairs= new Dictionary<Type, Type>();
        keyValuePairs.Add(typeof(FlowNode),typeof(NodePropertyBox));
        keyValuePairs.Add(typeof(FlowLink),typeof(LinkPropertyBox));
        keyValuePairs.Add(typeof(Definition),typeof(DefinitionPropertyBox));
    }
    public virtual void Reload<T>(T node)
    {
        _renderComponent = keyValuePairs[node!.GetType()];
        parameters["Data"] = node;
        parameters["OnChange"] = OnChange;

        var instance = dynProperty?.Instance;
        StateHasChanged();
        if (instance is not null)
        {
            Task.Run(() =>
            {
                Task.Delay(200);
                var component = (IPropertyBox<T>)instance;
                component.Refresh();

            });
        }
    }
      
  }
 
 
<style scoped>
.ant-tabs>.ant-tabs-nav .ant-tabs-nav-wrap, .ant-tabs>div>.ant-tabs-nav .ant-tabs-nav-wrap{
                            background:white;
                            border-bottom:1px solid #f0f0f0;
  }
  .row{
    margin-bottom:10px;
  }
  .col{vertical-align:central;}
 
  .col.l{ text-align:right; }
  .lable{text-align:right;margin-right:5px;}

 </style>
                     

